<?php

namespace app\blog\controller;

use app\Base;
use app\service\blog\IndexService;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\response\View;

class Index extends Base
{

    /**
     * 首页
     * @param IndexService $service
     * @return View
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function show(IndexService $service): View
    {
        $list = $service::getList();
        $list['page'] = 1;
        return view('index', ['data' => $list]);
    }

    /**
     * 列表分页
     * @param $page
     * @param IndexService $service
     * @return View
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function page($page, IndexService $service): View
    {
        $list = $service::getlist($page);
        $list['page'] = $page;
        return view('index', ['data' => $list]);
    }

    /**
     * 详情
     * @param $id
     * @param IndexService $service
     * @return View
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function info($id, IndexService $service): View
    {
        $res = $service::info((int)$id);
        if (!$res) {
            return view('home/404');
        }
        return view('info', ['data' => $res]);
    }

    /**
     * 分类
     * @param IndexService $service
     * @return View
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function categories(IndexService $service): View
    {
        $res = $service::categories();
        return view('categories', ['data' => $res]);
    }

    /**
     * 归档
     * @param IndexService $service
     * @return View
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function archives(IndexService $service): View
    {
        $res = $service::archives($this->params);
        return view('archives', ['data' => $res]);
    }

    /**
     * 标签
     * @param IndexService $service
     * @return View
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function tags(IndexService $service): View
    {
        $res = $service::tags();
        return view('tags', ['data' => $res]);
    }

    /**
     * 项目
     * @return View
     */
    public function project(): View
    {
        return view('project');
    }

    /**
     * 搜索
     * @return View
     */
    public function search(): View
    {
        return view('search');
    }
}
